<?php defined('SYSPATH') or die('No direct script access.');
/**
* Display Helper
*/
class Helper_DisplayItems {
	
	static function &factory() {
	   $o = new Helper_DisplayItems();
	  return $o;	
	}
	
  // Show Yes/No Value from data (1=Yes/image true.png and Else=No/image false.png)
	public function yesno_value($number, $format='image')
	{
		if($number == '1')
		{
			if($format == 'image'):
				$show_yes_no = 'true.png';
			elseif($format == 'text'): 
				$show_yes_no = 'Yes';
			else:
				$show_yes_no = 'Enter The Format (image / text)';
			endif;			
		} else {
			if($format == 'image'):
				$show_yes_no = 'false.png';
			elseif($format == 'text'): 
				$show_yes_no = 'No';
			else:
				$show_yes_no = 'Enter The Format (image / text)';
			endif;			
		}
		
		return $show_yes_no;
	}
	
  // Show Gender Value from Sex Value
	public function gender_value($sex_id)
	{
		if($sex_id == '1')
		{
			$show_gender_value = 'Male';
		} else {
			$show_gender_value = 'Female';
		}
		
		return $show_gender_value;
	}
	
  // Show Day Strings (Senin, Selasa, Rabu, Kamis, Jumat, Sabtu, Minggu)
	public function day_string($daystring)
	{
		switch($daystring)
		{
		  case '1':
		    $day_result = 'Senin';
		    break;
		  case '2':
		    $day_result = 'Selasa';
		    break;
		  case '3':
		    $day_result = 'Rabu';
		    break;
		  case '4':
		    $day_result = 'Kamis';
		    break;
		  case '5':
		    $day_result = 'Jumat';
		    break;
		  case '6':
		    $day_result = 'Sabtu';
		    break;
		  case '7':
		    $day_result = 'Minggu';
		    break;
		  default:
		    $day_result = '-';
		    break;
		}
		return $day_result;
	}
	
  // Show Month Strings (Januari, Febuari, Maret, April, Mei, Juni, Juli, Agustus, September, Oktober, November, Desember)
	public function month_string($monthstring)
	{
		switch($monthstring)
		{
		  case '1':
		    $month_result = 'Januari';
		    break;
		  case '2':
		    $month_result = 'Febuari';
		    break;
		  case '3':
		    $month_result = 'Maret';
		    break;
		  case '4':
		    $month_result = 'April';
		    break;
		  case '5':
		    $month_result = 'Mei';
		    break;
		  case '6':
		    $month_result = 'Juni';
		    break;
		  case '7':
		    $month_result = 'Juli';
		    break;
		  case '8':
		    $month_result = 'Agustus';
		    break;
		  case '9':
		    $month_result = 'September';
		    break;
		  case '10':
		    $month_result = 'Oktober';
		    break;
		  case '11':
		    $month_result = 'November';
		    break;
		  case '12':
		    $month_result = 'Desember';
		    break;
		  default:
		    $month_result = '-';
		    break;
		}
		return $month_result;
	}
	
  // Show User Role Value from User Role Key
	public function privilege_value($ruserroles_key)
	{
		
		
		$privilege = DB::select('value')
			->from('userroles')
			->where('key','=',$ruserroles_key)
			->limit(1)
			->execute();
		$show_value = $privilege->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '-';
		} else {
			$the_value =  $show_value[0]['value'];
		}
		
		return $the_value;
	}
	
	// Show  Value from table from ID
	public function show_value($id,$tabel,$show='value')
	{
		$queries = DB::select()
			->from($tabel)
			->where('id','=',$id)
			->limit(1)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '-';
		} else {
			$the_value =  $show_value[0][$show];
		}
		
		return $the_value;
	}
	
	// Show  MAX ID From Table users
	public function show_max_id2($key,$tabel,$show='value')
	{
		$queries = DB::select()
			->from($tabel)
			->where('userroles_key', '=', $key)
			->order_by('id', 'DESC')
			->limit(1)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '-';
		} else {
			$the_value =  $show_value[0][$show];
		}
		
		return $the_value;
	}
	
	// Show  MAX ID From Table
	public function show_max_id($tabel,$show='value')
	{
		$queries = DB::select()
			->from($tabel)
			->order_by('id', 'DESC')
			->limit(1)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '-';
		} else {
			$the_value =  $show_value[0][$show];
		}
		
		return $the_value;
	}

	public function show_id_by_acccodes($keys,$tabel,$show='value')
	{
		$queries = DB::select()
			->from($tabel)
			->where('keys','=',$keys)
			->limit(1)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '-';
		} else {
			$the_value =  $show_value[0][$show];
		}
		
		return $the_value;
	}

	public function last_transaction_number($invoices_id)
	{
		$queries = DB::select()
			->from('acctransactions')
			->where('accinvoices_id','=',$invoices_id)
			->execute();
		$show_value = $queries->count();
		
		if(empty($show_value))
		{
			$the_value =  '0';
		} else {
			$the_value =  $show_value;
		}
		
		return $the_value;
	}

	public function lapkeuangan_pengeluaran($id_spsc)
	{
		$queries = DB::select()
			->from('purchase_order')
			->where('id_spsc','=',$id_spsc)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '0';
		} else {
			$the_value =  $show_value[0]['total_pembayaran'];
		}
		
		return $the_value;
	}
	
	public function last_transaction($invoices_id,$value)
	{
		$queries = DB::select()
			->from('acctransactions')
			->where('accinvoices_id','=',$invoices_id)
			->order_by('id','DESC')
			->limit(1)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '0';
		} else {
			$the_value =  $show_value[0][$value];
		}
		
		return $the_value;
	}
	
	public function show_field($table,$limit,$order)
	{
		$dataarray = DB::select()
			->from($table);
			if(!is_null($limit)) {
				$dataarray = $dataarray->limit($limit);
			}
			if(!is_null($order)) {
			$dataarray = $dataarray->order_by('position', $order);
			}
			$dataarray = $dataarray->where('approve', '!=', NULL);
			$dataarray = $dataarray->execute();
			$defaultarrays = $dataarray->as_array();
		
		return $defaultarrays;
	}
	
	public function show_invoice_id($invoicenumbers)
	{
		$queries = DB::select()
			->from('accinvoices')
			->where('invoicenumbers','=',$invoicenumbers)
			->limit(1)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '-';
		} else {
			$the_value =  $show_value[0]['id'];
		}
		
		return $the_value;
	}
	
	public function show_invoicenumbers($invoices_id)
	{
		$queries = DB::select()
			->from('accinvoices')
			->where('id','=',$invoices_id)
			->limit(1)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '-';
		} else {
			$the_value =  $show_value[0]['invoicenumbers'];
		}
		
		return $the_value;
	}
	
	public function show_invoice_code($invoices_id)
	{
		$queries = DB::select()
			->from('accinvoices')
			->where('id','=',$invoices_id)
			->limit(1)
			->execute();
		$show_value = $queries->as_array();
		
		if(empty($show_value))
		{
			$the_value =  '-';
		} else {
			$the_value =  $show_value[0]['invoice_code'];
		}
		
		return $the_value;
	}

	public function count_jml($table,$id_master)
	{
		$queries = DB::select()
			->from($table)
			->where('id_master','=',$id_master)
			->execute()->as_array();
		
		if(!empty($queries))
		{
			$jml =0;
			foreach ($queries as $querie) {
				$jml += $querie['jumlah'];
			}
			$the_value =  $jml;
		} else {
			$the_value =  0;
		}
		
		return $the_value;
	}

	public function find_prod_id_from_autocomplete($product_name,$invoices_id)
	{ 
		$product_explode = explode("#",$product_name); 
		$string          = substr($product_explode[0], 0,1);
		$check_string    = (is_numeric($string) ? 'int' : 'not_int');

		$check_products = DB::select()
			->from('products');
			if($check_string == 'int')
			{
				$check_products = $check_products->where('barcode_pcs','=',$product_name);
			}
			if($check_string == 'not_int')
			{
				$check_products = $check_products->where('name','=',$product_explode[0]);
			}
			$check_products = $check_products->execute()->as_array();
		if(!empty($check_products)) {
			return $check_products[0]['id'];
		} else {
			//$this->message->store_message('c_fails');
		  	$r = Request::current();
		  	$r->redirect('pos/invoice?invoices_id='.$invoices_id);
		}
	}


	
	
	
	
	
	
	
	
	
	
}




